import Vue from 'vue'
import Router from 'vue-router'


import Auth from '@/modules/Auth'

import Notfound from '@/components/Notfound'
import AppLayout from '@/components/AppLayout'
import Dashboard from '@/components/Dashboard'
import SaleIntentionIndex from '@/components/SaleIntentionIndex'
import SaleIntentionEdit from '@/components/SaleIntentionEdit'
import SaleIntentionAssign from '@/components/SaleIntentionAssign'
import CustomDevIndex from '@/components/CustomDevIndex'
import CustomDevPlan from '@/components/CustomDevPlan'
import CustomIndex from '@/components/CustomIndex'
import CustomEdit from '@/components/CustomEdit'
import ContactIndex from '@/components/ContactIndex'
import ContactEdit from '@/components/ContactEdit'
import VisitIndex from '@/components/VisitIndex'
import VisitEdit from '@/components/VisitEdit'
import OrderIndex from '@/components/OrderIndex'
import OrderDetail from '@/components/OrderDetail'
import CustomLostIndex from '@/components/CustomLostIndex'
import CustomLostDelay from '@/components/CustomLostDelay'
import ServiceEdit from '@/components/ServiceEdit'
import ServiceAssignIndex from '@/components/ServiceAssignIndex'
import ServiceAssign from '@/components/ServiceAssign'
import ServiceDoIndex from '@/components/ServiceDoIndex'
import ServiceDo from '@/components/ServiceDo'
import ServiceResultIndex from '@/components/ServiceResultIndex'
import ServiceResult from '@/components/ServiceResult'
import ServiceArchiveIndex from '@/components/ServiceArchiveIndex'
import ServiceArchiveDetail from '@/components/ServiceArchiveDetail'
import BaseDataIndex from '@/components/BaseDataIndex'
import BaseDataEdit from '@/components/BaseDataEdit'
import RoleIndex from '@/components/RoleIndex'
import RoleEdit from '@/components/RoleEdit'
import UserIndex from '@/components/UserIndex'
import UserEdit from '@/components/UserEdit'
import ProductIndex from '@/components/ProductIndex'
import OperationLog from '@/components/OperationLog'
import ProfileIndex from '@/components/ProfileIndex'

import Login from '@/components/Login'

Vue.use(Router)

// 登录成功后默认跳转到的页面
var RouteDefault = {
    path: 'dashboard',name: 'Dashboard',component:Dashboard,
    /**
     * 路由配置级别上的路由钩子
     */
    beforeEnter(to,from,next){
        // console.log('RouteDefault beforeEnter');
        next();
    }
};
// 登录页面
var RouteLogin = {path:'/login',name:'Login',component:Login,meta:{auth:false}};

// 登录成功后的页面路由数组
var Routes = [
    RouteDefault,
    //营销管理里
    {path:'sale_intention/index',name:'SaleIntentionIndex',component:SaleIntentionIndex},
    {path:'sale_intention/ediit/:id',name:'SaleIntentionEdit',component:SaleIntentionEdit},
    {path:'sale_intention/assign/:id',name:'SaleIntentionAssign',component:SaleIntentionAssign},
    {path:'custom_dev/index',name:'CustomDevIndex',component:CustomDevIndex},
    {path:'custom_dev/plan/:id',name:'CustomDevPlan',component:CustomDevPlan},

    //客户管理
    {path:'custom/index',name:'CustomIndex',component:CustomIndex},
    {path:'custom/edit/:id',name:'CustomEdit',component:CustomEdit},
    {path:'contact/index',name:'ContactIndex',component:ContactIndex},
    {path:'contact/edit/:id',name:'ContactEdit',component:ContactEdit},
    {path:'visit/index',name:'VisitIndex',component:VisitIndex},
    {path:'visit/edit/:id',name:'VisitEdit',component:VisitEdit},
    {path:'order/index',name:'OrderIndex',component:OrderIndex},
    {path:'order/edit/:id',name:'OrderDetail',component:OrderDetail},
    {path:'custom/lost/index',name:'CustomLostIndex',component:CustomLostIndex},
    {path:'custom/lost/delay/:id',name:'CustomLostDelay',component:CustomLostDelay},

    //服务管理
    {path:'service/edit/:id',name:'ServiceEdit',component:ServiceEdit},
    {path:'service/assign/list',name:'ServiceAssignIndex',component:ServiceAssignIndex},
    {path:'service/assign/:id',name:'ServiceAssign',component:ServiceAssign},
    {path:'service/do/list',name:'ServiceDoIndex',component:ServiceDoIndex},
    {path:'service/do/:id',name:'ServiceDo',component:ServiceDo},
    {path:'service/result/list',name:'ServiceResultIndex',component:ServiceResultIndex},
    {path:'service/result/:id',name:'ServiceResult',component:ServiceResult},
    {path:'service/archive/list',name:'ServiceArchiveIndex',component:ServiceArchiveIndex},
    {path:'service/archive/:id',name:'ServiceArchiveDetail',component:ServiceArchiveDetail},

    //系统管理
    {path:'basedata/index',name:'BaseDataIndex',component:BaseDataIndex},
    {path:'basedata/edit/:id',name:'BaseDataEdit',component:BaseDataEdit},
    {path:'role/index',name:'RoleIndex',component:RoleIndex},
    {path:'role/edit/:id',name:'RoleEdit',component:RoleEdit},
    {path:'user/index',name:'UserIndex',component:UserIndex},
    {path:'user/edit/:id',name:'UserEdit',component:UserEdit},
    {path:'product/index',name:'ProductIndex',component:ProductIndex},
    {path:'oplog/index',name:'OperationLog',component:OperationLog},

    //profile
    {path:'profile/index',name:'ProfileIndex',component:ProfileIndex},

];

// 定义路由组件
var router = new Router({
    mode:'history',
    routes: [
        RouteLogin,
        {path:'/crm',name: 'AppLayout',component:AppLayout,children:Routes},
        {path:'*',component: Notfound}
    ]
});



/**
 * 全局路由钩子
 * 路由被匹配后，在进入之前被调用，可以用作验证用户有没有权限访问该页面
 * 可以影响最终的页面跳转
 */
router.beforeEach(function(to,from,next){
    console.log('router beforeEach');
    //认证检测
    if(!(to.meta.auth == false)){
        if(!Auth.check()){
            router.push(RouteLogin);
            return;
        }
    }

    if(to.path == '/'){
        router.push(RouteDefault);
        return;
    }
    next();
});


router.beforeResolve(function(to,from,next){
    console.log('router beforeResolve');
    next();
});


router.afterEach(function(to,from){
    console.log('router afterEach');
});

export default router;
